package com.tengju.user.domain.model.survey;

import com.tengju.user.domain.shared.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Value
public class SurveyQuestionConfig {

    /** 问题code **/
    private String code;

    /** 问题名称 **/
    private String title;

    /** 问题顺序order **/
    @NonFinal
    private Integer order;

    private SurveyQuestionTypeEnum type;

    /** 是否必选 **/
    private SurveyQuestionRequireEnum required;

    /** 问题选项 **/
    private List<SurveyQuestionConfigContent> configContentList;


    private MsgType msgType;


    public SurveyQuestionConfig(String itemCode, String itemTitle, Integer itemOrder, SurveyQuestionTypeEnum surveyQuestionTypeEnum, SurveyQuestionRequireEnum surveyQuestionRequireEnum, String itemParamStr, MsgType msgType) {
        this.code = itemCode;
        this.title = itemTitle;
        this.order = itemOrder;
        this.type = surveyQuestionTypeEnum;
        this.required = surveyQuestionRequireEnum;
        this.configContentList = parseParams(itemParamStr);
        this.msgType = msgType;
    }

    private List<SurveyQuestionConfigContent> parseParams(String itemParams) {
        if (StringUtils.isBlank(itemParams)) {
            return Collections.emptyList();
        }
        return JsonUtil.toList(itemParams,new TypeReference<List<SurveyQuestionConfigContent>>(){});
    }

}
